﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.IO;

namespace Flashloader_Core
{
    internal class Parameter
    {
        public string SerialPortNum { get; set; }
        public string SecretCode { get; set; }
        public string FilePath { get; set; }
        public bool EmergencyFlag { get; set; }
        
        public uint ParamListCheck(string[] args)
        {
            uint uRet = 0xFF;
            if ((args.Length != 3) && (args.Length != 4))
            {
                Console.WriteLine("请输入正确的参数");
            }
            else
            {
                if (args[0].StartsWith("-pCOM"))
                {
                    SerialPortNum = args[0].Substring(2);
                    if (args[1].StartsWith("-s"))
                    {
                        SecretCode = args[1].Substring(2);

                        if (args[2].StartsWith("-f"))
                        {
                            FilePath = args[2].Substring(2);
                            if ((args.Length == 4) && args[3].StartsWith("-e"))
                            {
                                EmergencyFlag = true;
                            }
                            else
                            {
                                EmergencyFlag = false;
                            }
                            uRet = 0x00;
                        }
                        else
                        {
                            Console.WriteLine("请指定正确的bin文件路径，并且与'-f'前缀进行拼接，如'-f./example.bin'");
                        }
                    }
                    else
                    {
                        Console.WriteLine("请输入争取的编程密钥，并且与'-s'前缀进行拼接，如'-s01020304'!");
                    }
                }
                else
                {
                    Console.WriteLine("请指定正确的串口号，并且与'-p'前缀进行拼接，如-pCOMx!");
                }

            }

            if (uRet == 0x00)
            {
                SerialPort TestPort = new SerialPort();
                TestPort.PortName = SerialPortNum;
                if (TestPort.IsOpen == true)
                {
                    Console.WriteLine("串口：" + TestPort.PortName + "已经被占用，请重新选择串口！");
                    uRet = 0xFE;

                }
                else
                {
                    try
                    {
                        TestPort.Open();
                        uRet = 0x00;
                        
                        Console.WriteLine("串口：" + TestPort.PortName + "检测成功...");
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine("串口：" + TestPort.PortName + "打开失败");
                        Console.WriteLine("详细错误：");
                        Console.WriteLine(ex.ToString());
                        uRet = 0xFD;
                    }
                    TestPort.Close();
                }
            }
            if (uRet == 0x00)
            {
                if (File.Exists(FilePath) == true)
                {
                    Console.WriteLine("程序文件：" + FilePath + "检测成功...");
                }
                else
                {
                    Console.WriteLine("无法检测到程序文件：" + FilePath);
                    uRet = 0xFC;
                }
            }

            return uRet;
        }


    }
}
